package com.car.controller;

import com.car.entity.Product;
import com.car.service.impl.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    @Qualifier("productService")
    private ProductService productService;
    //商品展示
    @RequestMapping(value = "/findProduct")
    public String findProduct(Model model){
        List<Product> product = productService.findProduct();
        model.addAttribute("product",product);
        return "/goods_List.jsp";
    }
    //修改状态
    @RequestMapping("/updateStatus")
    public String updateStatus(int id,int shelfId){
        Product product = new Product();
        product.setStatus(String.valueOf(shelfId));
        product.setPid(id);
        productService.updateStatus(product);
        return "redirect:/findProduct";
    }
    //删除商品
    @RequestMapping(value = "/deleteProduct")
    public String deleteProduct(int pid){
        productService.deleteProduct(pid);
        return "redirect:/findProduct";
    }
    //通过id查询商品
    @RequestMapping(value = "/findProductById")
    public String findProductById(int id, Model model){
        Product list = productService.findProductById(id);
        List<Product> kind = productService.findKind();
        model.addAttribute("kind",kind);
        model.addAttribute("list",list);
        return "edit_Goods.jsp";
    }

    //修改商品
    @RequestMapping(value = "/updateProduct",method = RequestMethod.POST)
    public String upadateProduct(Product product){
        productService.updateProduct(product);
        return "redirect:/findProduct";
    }
    //增加商品
    @RequestMapping("/addProduct")
    public String addProduct(Product product){
        product.setRegtime(new Date());
        productService.addProduct(product);
        return "redirect:/findProduct";
    }
    //查询消耗管理
    @RequestMapping(value = "/findXH")
    public String findXH(Model model){
        List<Product> product = productService.findXH();
        model.addAttribute("xh",product);
        return "/Expenses.jsp";
    }
    //增加消耗管理
    @RequestMapping(value = "addXH")
    public String addXH(Product product){
        product.setSx("1");
        productService.addXH(product);
        return "redirect:/findXH";
    }
    //通过id查入库
    @RequestMapping(value = "/findRKID")
    public String findRKID(int pid,Model model){
        Product list = productService.findProductById(pid);
        model.addAttribute("list",list);
        return "sign_In.jsp";
    }
    //通过id查出库
    @RequestMapping(value = "/findCKID")
    public String findCKID(int pid,Model model){
        Product list = productService.findProductById(pid);
        model.addAttribute("list",list);
        return "sign_Out.jsp";
    }
    //编辑入库
    @RequestMapping(value = "/UPRUKU")
    public String RUKU(Product product){
        product.setDigit("入库"+product.getDigits()+"件");
        product.setRegtime(new Date());
        productService.updateDigit(product);
        productService.updateRUKU(product);
        return "redirect:/findXH";
    }
    //编辑出库
    @RequestMapping(value = "/CHUKU")
    public String CHUKU(Product product){
        product.setDigit("出库"+product.getDigits()+"件");
        product.setRegtime(new Date());
        productService.updateDigit(product);
        productService.updateCHUKU(product);
        return "redirect:/findXH";
    }

    //查询出入库
    @RequestMapping(value = "/findCR")
    public String findCR(Model model){
        Product product = new Product();
        product.setRegtime(new Date());
        List<Product> cr = productService.findCR();
        model.addAttribute("cr",cr);
        return "expenses_Record.jsp";
    }

    //通过时间查询出入库信息
    @RequestMapping(value = "/findByName")
    public String findByName(Model model,String regtime,String churegtime,String sx){
        //全部记录  入库记录 出库记录
        List<Product> cr = productService.selectCR(regtime,churegtime,sx);
        model.addAttribute("cr",cr);
        return "expenses_Record.jsp";
    }
}
